package aes.client;

import javax.swing.*;
import javax.swing.border.*;

import common.CommandMessage;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class CreateUserUI extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private JTextField txtLogin;
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private JTextField txtPassword;
	private JTextField txtRole;
	private JButton btnCreate;
	private JButton btnCancel;
	
	private Client c;
	private ClientUI ui;
	
	public CreateUserUI(Client c, ClientUI ui) {
		this.c=c;
		this.ui = ui;
		setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(116, 57, 278, 253);
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Create User");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel.setBounds(85, 11, 89, 14);
		panel_1.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Login:");
		lblNewLabel_1.setBounds(10, 48, 46, 14);
		panel_1.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("First Name:");
		lblNewLabel_2.setBounds(10, 82, 65, 14);
		panel_1.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Last Name:");
		lblNewLabel_3.setBounds(10, 116, 65, 14);
		panel_1.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("Password:");
		lblNewLabel_4.setBounds(10, 148, 65, 14);
		panel_1.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("Role:");
		lblNewLabel_5.setBounds(10, 180, 65, 14);
		panel_1.add(lblNewLabel_5);
		
		txtLogin = new JTextField();
		txtLogin.setBounds(123, 45, 145, 20);
		panel_1.add(txtLogin);
		txtLogin.setColumns(10);
		
		txtFirstName = new JTextField();
		txtFirstName.setColumns(10);
		txtFirstName.setBounds(123, 79, 145, 20);
		panel_1.add(txtFirstName);
		
		txtLastName = new JTextField();
		txtLastName.setColumns(10);
		txtLastName.setBounds(123, 113, 145, 20);
		panel_1.add(txtLastName);
		
		txtPassword = new JTextField();
		txtPassword.setColumns(10);
		txtPassword.setBounds(123, 145, 145, 20);
		panel_1.add(txtPassword);
		
		txtRole = new JTextField();
		txtRole.setColumns(10);
		txtRole.setBounds(123, 177, 145, 20);
		panel_1.add(txtRole);
		
		btnCreate = new JButton("Create");
		btnCreate.setBounds(44, 227, 89, 23);
		btnCreate.addActionListener(this);
		panel_1.add(btnCreate);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(143, 227, 89, 23);
		btnCancel.addActionListener(this);
		panel_1.add(btnCancel);
		

	}

	@Override
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnCreate)) {
			// 
			try {
				// Check if fields are not empty.
				String login = txtLogin.getText().trim();
				String firstName = txtFirstName.getText().trim();
				String lastName = txtLastName.getText().trim();
				String password = txtPassword.getText().trim();
				String role = txtRole.getText().trim();
				if (login.length() < 1 || firstName.length() < 1 || lastName.length() < 1 || password.length() < 1 || role.length() < 1) {
					ClientUI.ShowMessageBox("Error", "Please fill all fields!", JOptionPane.ERROR_MESSAGE);
					return;
				}
				// Prepare login message.
				CommandMessage msg = new CommandMessage(CommandMessage.CREATE_USER,new String[] {login, firstName, lastName,password,role});
				// Send login request.
				c.sendToServer(msg);
			} catch (IOException ex) {
				ex.printStackTrace();
				ClientUI.ShowMessageBox("Error", "Creation Failed", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource().equals(btnCancel)) {
			ui.SetContent(ClientUI.LOGIN_SCREEN);
		}
		
	}
}
